Recap We are analysing data about complaints for the presence of cocroaches in 10 residential buildings. Using a Bayesian framework, we modelled the number of complaints using a Poisson model. Diagnostic analysis of the model highlights some residual variability. We decided to try to improve our estimates adding one predictor and an exposure term to the Poisson model, but the situation did not improve. The ‘Poisson’ assumption on the model variance turned out to be too restrictive to model our data. The Negative Binomial model gave better results, but examining the standardized residual plot we noticed that some of them were still very large.
A posterior predictive check considering that the data are clustered by building..
library(rstan)
library(dplyr)
library(lubridate)
library(ggplot2)
library(bayesplot)
theme_set(bayesplot::theme_default())
rstan_options(auto_write=TRUE)
pest_data <- readRDS('pest_data.RDS')
str(pest_data)
## 'data.frame': 120 obs. of 14 variables:
## $ mus : num 0.369 0.359 0.282 0.129 0.452 ...
## $ building_id : int 37 37 37 37 37 37 37 37 37 37 ...
## $ wk_ind : int 1 2 3 4 5 6 7 8 9 10 ...
## $ date : Date, format: "2017-01-15" "2017-02-14" ...
## $ traps : num 8 8 9 10 11 11 10 10 9 9 ...
## $ floors : num 8 8 8 8 8 8 8 8 8 8 ...
## $ sq_footage_p_floor : num 5149 5149 5149 5149 5149 ...
## $ live_in_super : num 0 0 0 0 0 0 0 0 0 0 ...
## $ monthly_average_rent: num 3847 3847 3847 3847 3847 ...
## $ average_tenant_age : num 53.9 53.9 53.9 53.9 53.9 ...
## $ age_of_building : num 47 47 47 47 47 47 47 47 47 47 ...
## $ total_sq_foot : num 41192 41192 41192 41192 41192 ...
## $ month : num 1 2 3 4 5 6 7 8 9 10 ...
## $ complaints : num 1 3 0 1 0 0 4 3 2 2 ...
summary(pest_data)
## mus building_id wk_ind date
## Min. :-0.1008 Min. : 5.0 Min. : 1.00 Min. :2017-01-15
## 1st Qu.: 0.5279 1st Qu.:26.0 1st Qu.: 3.75 1st Qu.:2017-04-07
## Median : 1.0422 Median :46.0 Median : 6.50 Median :2017-06-29
## Mean : 1.1116 Mean :49.6 Mean : 6.50 Mean :2017-06-29
## 3rd Qu.: 1.6837 3rd Qu.:70.0 3rd Qu.: 9.25 3rd Qu.:2017-09-19
## Max. : 2.8030 Max. :98.0 Max. :12.00 Max. :2017-12-11
## traps floors sq_footage_p_floor live_in_super
## Min. : 1.000 Min. : 4.0 Min. :4186 Min. :0.0
## 1st Qu.: 6.000 1st Qu.: 8.0 1st Qu.:4770 1st Qu.:0.0
## Median : 7.000 Median :10.0 Median :5097 Median :0.0
## Mean : 7.033 Mean : 9.9 Mean :4991 Mean :0.3
## 3rd Qu.: 8.000 3rd Qu.:13.0 3rd Qu.:5206 3rd Qu.:1.0
## Max. :11.000 Max. :15.0 Max. :5740 Max. :1.0
## monthly_average_rent average_tenant_age age_of_building total_sq_foot
## Min. :3029 Min. :41.14 Min. :39.0 Min. :19217
## 1st Qu.:3564 1st Qu.:45.14 1st Qu.:47.0 1st Qu.:41192
## Median :3813 Median :48.20 Median :49.0 Median :47096
## Mean :3687 Mean :49.92 Mean :49.4 Mean :49248
## 3rd Qu.:3864 3rd Qu.:53.88 3rd Qu.:51.0 3rd Qu.:59251
## Max. :4019 Max. :65.18 Max. :60.0 Max. :78093
## month complaints
## Min. : 1.00 Min. : 0.000
## 1st Qu.: 3.75 1st Qu.: 1.000
## Median : 6.50 Median : 2.000
## Mean : 6.50 Mean : 3.658
## 3rd Qu.: 9.25 3rd Qu.: 5.250
## Max. :12.00 Max. :18.000
##number of buildings
N_buildings <- length(unique(pest_data$building_id))
N_buildings
## [1] 10
## arrange data into a list
stan_dat_simple <- list(
N = nrow(pest_data),
complaints = pest_data$complaints,
traps = pest_data$traps,
log_sq_foot = log(pest_data$total_sq_foot/1e4),
live_in_super = pest_data$live_in_super
)
str(stan_dat_simple)
## List of 5
## $ N : int 120
## $ complaints : num [1:120] 1 3 0 1 0 0 4 3 2 2 ...
## $ traps : num [1:120] 8 8 9 10 11 11 10 10 9 9 ...
## $ log_sq_foot : num [1:120] 1.42 1.42 1.42 1.42 1.42 ...
## $ live_in_super: num [1:120] 0 0 0 0 0 0 0 0 0 0 ...
comp_model_NB <- stan_model('multiple_NB_regression.stan')
fitted_model_NB <- sampling(comp_model_NB, data = stan_dat_simple)
##
## SAMPLING FOR MODEL 'multiple_NB_regression' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 7e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.7 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.972347 seconds (Warm-up)
## Chain 1: 0.873229 seconds (Sampling)
## Chain 1: 1.84558 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'multiple_NB_regression' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 0.000113 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 1.13 seconds.
## Chain 2: Adjust your expectations accordingly!
## Chain 2:
## Chain 2:
## Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 2:
## Chain 2: Elapsed Time: 0.959071 seconds (Warm-up)
## Chain 2: 0.927717 seconds (Sampling)
## Chain 2: 1.88679 seconds (Total)
## Chain 2:
##
## SAMPLING FOR MODEL 'multiple_NB_regression' NOW (CHAIN 3).
## Chain 3:
## Chain 3: Gradient evaluation took 4.2e-05 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.42 seconds.
## Chain 3: Adjust your expectations accordingly!
## Chain 3:
## Chain 3:
## Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 3:
## Chain 3: Elapsed Time: 0.959271 seconds (Warm-up)
## Chain 3: 0.759426 seconds (Sampling)
## Chain 3: 1.7187 seconds (Total)
## Chain 3:
##
## SAMPLING FOR MODEL 'multiple_NB_regression' NOW (CHAIN 4).
## Chain 4:
## Chain 4: Gradient evaluation took 0.000126 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 1.26 seconds.
## Chain 4: Adjust your expectations accordingly!
## Chain 4:
## Chain 4:
## Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 4:
## Chain 4: Elapsed Time: 0.935087 seconds (Warm-up)
## Chain 4: 0.791986 seconds (Sampling)
## Chain 4: 1.72707 seconds (Total)
## Chain 4:
samps_NB <- rstan::extract(fitted_model_NB)
y_rep <- samps_NB$y_rep
ppc_stat_grouped(
y = stan_dat_simple$complaints,
yrep = y_rep,
group = pest_data$building_id,
stat = 'mean',
binwidth = 0.2
)
5 Select some of the variables that give information at the building level (live in super, age of building, average tentant age, and monthly average rent) and arrange them into a matrix called building_data.
N_months <- length(unique(pest_data$date))
# Add some IDs for building and month
pest_data <- pest_data %>%
mutate(
building_fac = factor(building_id, levels = unique(building_id)),
building_idx = as.integer(building_fac),
ids = rep(1:N_months, N_buildings),
mo_idx = lubridate::month(date)
)
# Center and rescale the building specific data
building_data <- pest_data %>%
select(
building_idx,
live_in_super,
age_of_building,
total_sq_foot,
average_tenant_age,
monthly_average_rent
) %>%
unique() %>%
arrange(building_idx) %>%
select(-building_idx) %>%
scale(scale=FALSE) %>%
as.data.frame() %>%
mutate( # scale by constants
age_of_building = age_of_building / 10,
total_sq_foot = total_sq_foot / 10000,
average_tenant_age = average_tenant_age / 10,
monthly_average_rent = monthly_average_rent / 1000
) %>%
as.matrix()
str(building_data)
## num [1:10, 1:5] -0.3 -0.3 -0.3 -0.3 0.7 -0.3 -0.3 0.7 -0.3 0.7 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:5] "live_in_super" "age_of_building" "total_sq_foot" "average_tenant_age" ...
stan_dat_hier <-
with(pest_data,
list(complaints = complaints,
traps = traps,
N = length(traps),
J = N_buildings,
log_sq_foot = log(pest_data$total_sq_foot/1e4),
building_data = building_data[,-3],
mo_idx = as.integer(as.factor(date)),
K = 4,
building_idx = building_idx
)
)
str(stan_dat_hier)
## List of 9
## $ complaints : num [1:120] 1 3 0 1 0 0 4 3 2 2 ...
## $ traps : num [1:120] 8 8 9 10 11 11 10 10 9 9 ...
## $ N : int 120
## $ J : int 10
## $ log_sq_foot : num [1:120] 1.42 1.42 1.42 1.42 1.42 ...
## $ building_data: num [1:10, 1:4] -0.3 -0.3 -0.3 -0.3 0.7 -0.3 -0.3 0.7 -0.3 0.7 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr [1:4] "live_in_super" "age_of_building" "average_tenant_age" "monthly_average_rent"
## $ mo_idx : int [1:120] 1 2 3 4 5 6 7 8 9 10 ...
## $ K : num 4
## $ building_idx : int [1:120] 1 1 1 1 1 1 1 1 1 1 ...
6 Model the variation across buildings usign a varying intercept Negative Binomial hierarchical model.
Using stan write, compile and fit the following varying intercept model
\[ \text{complaints}_{ib} \sim \text{Neg-Binomial}(\lambda_{ib}, \phi) \\ \lambda_{ib} = \exp{(\eta_{ib})} \\ \eta_{ib} = \mu_{b(i)} + \beta \, {\rm traps}_{i} + \text{log_sq_foot}_i \\ \mu_b \sim \text{Normal}(\alpha + \beta_{\rm super}\, {\rm super}_b + \ldots + \beta_{\rm mar}\, {\rm mar}_b , \sigma_{\mu}) \]
comp_model_NB_hier <- stan_model('hier_NB_regression.stan')
fitted_model_NB_hier <- sampling(comp_model_NB_hier, data = stan_dat_hier,
chains = 4, cores = 4, iter = 4000)
7 Do you have any divergent transitions?
samps_hier_NB <- rstan::extract(fitted_model_NB_hier)
print(fitted_model_NB_hier, pars = c('sigma_mu','beta','alpha','phi','mu'))
## Inference for Stan model: hier_NB_regression.
## 4 chains, each with iter=4000; warmup=2000; thin=1;
## post-warmup draws per chain=2000, total post-warmup draws=8000.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## sigma_mu 0.25 0.03 0.17 0.05 0.12 0.22 0.34 0.67 39 1.08
## beta -0.24 0.01 0.06 -0.34 -0.29 -0.24 -0.20 -0.11 57 1.07
## alpha 1.32 0.06 0.44 0.44 1.01 1.32 1.68 2.06 51 1.07
## phi 1.58 0.01 0.32 1.06 1.37 1.57 1.75 2.36 2388 1.00
## mu[1] 1.32 0.05 0.54 0.17 0.97 1.36 1.73 2.26 133 1.04
## mu[2] 1.28 0.05 0.53 0.20 0.91 1.31 1.71 2.22 106 1.04
## mu[3] 1.49 0.08 0.50 0.49 1.14 1.48 1.92 2.32 43 1.08
## mu[4] 1.48 0.02 0.46 0.54 1.18 1.52 1.77 2.36 539 1.02
## mu[5] 1.14 0.07 0.44 0.24 0.84 1.14 1.49 1.88 38 1.09
## mu[6] 1.24 0.09 0.52 0.21 0.89 1.24 1.64 2.05 31 1.11
## mu[7] 1.51 0.04 0.50 0.50 1.17 1.53 1.89 2.42 161 1.04
## mu[8] 1.31 0.06 0.43 0.44 1.01 1.31 1.68 2.03 56 1.07
## mu[9] 1.46 0.08 0.57 0.26 1.07 1.48 1.94 2.40 49 1.07
## mu[10] 0.91 0.06 0.39 0.16 0.64 0.90 1.23 1.56 44 1.07
##
## Samples were drawn using NUTS(diag_e) at Thu May 16 13:44:12 2019.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
mcmc_trace(
as.array(fitted_model_NB_hier,pars = 'sigma_mu'),
np = nuts_params(fitted_model_NB_hier),
window = c(500,1000)
)
# assign to object so we can compare to another plot later
scatter_with_divs <- mcmc_scatter(
as.array(fitted_model_NB_hier),
pars = c("mu[4]", 'sigma_mu'),
transform = list('sigma_mu' = "log"),
np = nuts_params(fitted_model_NB_hier)
)
scatter_with_divs
N_sims <- 1000
log_sigma <- rep(NA, N_sims)
theta <- rep(NA, N_sims)
for (j in 1:N_sims) {
log_sigma[j] <- rnorm(1, mean = 0, sd = 1)
theta[j] <- rnorm(1, mean = 0, sd = exp(log_sigma[j]))
}
draws <- cbind("mu" = theta, "log(sigma_mu)" = log_sigma)
mcmc_scatter(draws)
parcoord_with_divs <- mcmc_parcoord(
as.array(fitted_model_NB_hier, pars = c("sigma_mu", "mu")),
np = nuts_params(fitted_model_NB_hier)
)
parcoord_with_divs
8 We see evidence that our problems concentrate when \(\texttt{sigma_mu}\) is small. Reparametrize the model using the non-centered parametrization and check diagnostics. The distribution of \(\texttt{mu}\) does not change: \[ \mu_b \sim \text{Normal}(\alpha + \texttt{building_data} \, \zeta, \sigma_{\mu}) \] We add an auxiliary variable in the parameters block \(\texttt{mu_raw}\sim \text{Normal}(0, 1)\) and we move \(\texttt{mu}\) in the transformed parameters block.
transformed parameters {
vector[J] mu;
mu = alpha + building_data * zeta + sigma_mu * mu_raw;
}
comp_model_NB_hier_ncp <- stan_model('hier_NB_regression_ncp.stan')
fitted_model_NB_hier_ncp <- sampling(comp_model_NB_hier_ncp, data = stan_dat_hier, chains = 4, cores = 4)
print(fitted_model_NB_hier_ncp, pars = c('sigma_mu','beta','alpha','phi','mu'))
## Inference for Stan model: hier_NB_regression_ncp.
## 4 chains, each with iter=2000; warmup=1000; thin=1;
## post-warmup draws per chain=1000, total post-warmup draws=4000.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## sigma_mu 0.23 0.00 0.17 0.01 0.10 0.19 0.32 0.65 1247 1
## beta -0.23 0.00 0.06 -0.36 -0.27 -0.23 -0.19 -0.12 2611 1
## alpha 1.26 0.01 0.44 0.45 0.96 1.25 1.55 2.17 2584 1
## phi 1.59 0.01 0.36 1.02 1.33 1.55 1.80 2.40 4008 1
## mu[1] 1.28 0.01 0.55 0.24 0.91 1.27 1.64 2.38 2623 1
## mu[2] 1.23 0.01 0.52 0.22 0.87 1.21 1.57 2.28 2624 1
## mu[3] 1.40 0.01 0.49 0.47 1.06 1.38 1.72 2.43 2966 1
## mu[4] 1.43 0.01 0.49 0.53 1.09 1.43 1.76 2.40 2733 1
## mu[5] 1.09 0.01 0.43 0.25 0.81 1.08 1.36 1.93 3244 1
## mu[6] 1.18 0.01 0.50 0.24 0.85 1.17 1.52 2.18 2686 1
## mu[7] 1.47 0.01 0.51 0.49 1.11 1.46 1.81 2.51 2963 1
## mu[8] 1.25 0.01 0.43 0.42 0.96 1.24 1.54 2.13 3204 1
## mu[9] 1.43 0.01 0.57 0.32 1.06 1.43 1.80 2.56 2667 1
## mu[10] 0.87 0.01 0.37 0.17 0.61 0.86 1.11 1.62 3502 1
##
## Samples were drawn using NUTS(diag_e) at Thu May 16 13:44:25 2019.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
scatter_no_divs <- mcmc_scatter(
as.array(fitted_model_NB_hier_ncp),
pars = c("mu[4]", 'sigma_mu'),
transform = list('sigma_mu' = "log"),
np = nuts_params(fitted_model_NB_hier_ncp)
)
bayesplot_grid(scatter_with_divs, scatter_no_divs,
grid_args = list(ncol = 2), ylim = c(-11, 1))
parcoord_no_divs <- mcmc_parcoord(
as.array(fitted_model_NB_hier_ncp, pars = c("sigma_mu", "mu")),
np = nuts_params(fitted_model_NB_hier_ncp)
)
bayesplot_grid(parcoord_with_divs, parcoord_no_divs,
ylim = c(-3, 3))
samps_NB_hier_ncp <- rstan::extract(fitted_model_NB_hier_ncp, pars = c('y_rep','inv_phi'))
y_rep <- as.matrix(fitted_model_NB_hier_ncp, pars = "y_rep")
ppc_dens_overlay(stan_dat_hier$complaints, y_rep[1:200,])
ppc_stat_grouped(
y = stan_dat_hier$complaints,
yrep = y_rep,
group = pest_data$building_id,
stat = 'mean',
binwidth = 0.5
)
ppc_stat_grouped(
y = stan_dat_hier$complaints,
yrep = y_rep,
group = pest_data$building_id,
stat = 'sd',
binwidth = 0.5
)
ppc_intervals(
y = stan_dat_hier$complaints,
yrep = y_rep,
x = stan_dat_hier$traps
) +
labs(x = "Number of traps", y = "Number of complaints")
mean_y_rep <- colMeans(y_rep)
mean_inv_phi <- mean(as.matrix(fitted_model_NB_hier_ncp, pars = "inv_phi"))
std_resid <- (stan_dat_hier$complaints - mean_y_rep) / sqrt(mean_y_rep + mean_y_rep^2*mean_inv_phi)
qplot(mean_y_rep, std_resid) + hline_at(2) + hline_at(-2)
9 Try to expand the previous model with varying slope.
\[ \text{complaints}_{ib} \sim \text{Neg-Binomial}(\lambda_{ib}, \phi) \\ \lambda_{ib} = \exp{(\eta_{ib})}\\ \eta_{ib} = \mu_{b(i)} + \kappa_b \, \texttt{traps}_{ib} + \text{log_sq_foot}_i \\ \mu_b \sim \text{Normal}(\alpha + \texttt{building_data} \, \zeta, \sigma_{\mu}) \\ \kappa_b \sim \text{Normal}(\beta + \texttt{building_data} \, \gamma, \sigma_{\kappa}) \]
comp_model_NB_hier_slopes <- stan_model('hier_NB_regression_ncp_slopes_mod.stan')
fitted_model_NB_hier_slopes <-
sampling(
comp_model_NB_hier_slopes,
data = stan_dat_hier,
chains = 4, cores = 4,
control = list(adapt_delta = 0.95)
)
mcmc_hist(
as.matrix(fitted_model_NB_hier_slopes, pars = "sigma_kappa"),
binwidth = 0.005
)
print(fitted_model_NB_hier_slopes, pars = c('kappa','beta','alpha','phi','sigma_mu','sigma_kappa','mu'))
## Inference for Stan model: hier_NB_regression_ncp_slopes_mod.
## 4 chains, each with iter=2000; warmup=1000; thin=1;
## post-warmup draws per chain=1000, total post-warmup draws=4000.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## kappa[1] -0.22 0.00 0.09 -0.40 -0.28 -0.22 -0.16 -0.04 2640 1
## kappa[2] -0.21 0.00 0.10 -0.40 -0.28 -0.22 -0.15 -0.02 2787 1
## kappa[3] -0.18 0.00 0.14 -0.45 -0.27 -0.18 -0.08 0.09 2964 1
## kappa[4] -0.15 0.00 0.10 -0.34 -0.22 -0.15 -0.08 0.06 3382 1
## kappa[5] -0.26 0.00 0.11 -0.47 -0.33 -0.26 -0.18 -0.05 3290 1
## kappa[6] -0.20 0.00 0.10 -0.41 -0.27 -0.20 -0.13 0.00 3019 1
## kappa[7] -0.10 0.00 0.13 -0.36 -0.19 -0.11 -0.01 0.15 3734 1
## kappa[8] -0.35 0.00 0.18 -0.72 -0.47 -0.35 -0.23 0.01 2604 1
## kappa[9] -0.19 0.00 0.14 -0.47 -0.28 -0.19 -0.10 0.09 2524 1
## kappa[10] -0.30 0.00 0.11 -0.52 -0.37 -0.30 -0.23 -0.09 6491 1
## beta -0.22 0.00 0.07 -0.37 -0.27 -0.22 -0.17 -0.07 2697 1
## alpha 1.11 0.01 0.51 0.10 0.76 1.11 1.45 2.13 2742 1
## phi 1.60 0.01 0.36 1.04 1.34 1.55 1.81 2.42 4281 1
## sigma_mu 0.29 0.01 0.23 0.01 0.11 0.24 0.40 0.89 1840 1
## sigma_kappa 0.04 0.00 0.04 0.00 0.02 0.03 0.06 0.15 1425 1
## mu[1] 1.14 0.02 0.80 -0.50 0.63 1.14 1.67 2.68 2548 1
## mu[2] 1.10 0.02 0.81 -0.48 0.55 1.11 1.64 2.70 2697 1
## mu[3] 1.10 0.02 0.95 -0.74 0.46 1.09 1.73 2.91 3034 1
## mu[4] 0.95 0.01 0.69 -0.41 0.48 0.95 1.43 2.32 3409 1
## mu[5] 1.20 0.01 0.71 -0.17 0.73 1.21 1.68 2.59 3198 1
## mu[6] 0.89 0.01 0.71 -0.52 0.42 0.88 1.38 2.31 2939 1
## mu[7] 0.57 0.02 0.99 -1.34 -0.11 0.57 1.22 2.47 3672 1
## mu[8] 1.93 0.02 1.02 -0.03 1.25 1.90 2.60 3.99 2823 1
## mu[9] 1.01 0.02 1.04 -1.10 0.32 1.04 1.71 3.07 2551 1
## mu[10] 1.13 0.01 0.53 0.16 0.77 1.10 1.47 2.21 6671 1
##
## Samples were drawn using NUTS(diag_e) at Thu May 16 13:44:49 2019.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
mcmc_hist(
as.matrix(fitted_model_NB_hier_slopes, pars = "beta"),
binwidth = 0.005
)
y_rep <- as.matrix(fitted_model_NB_hier_slopes, pars = "y_rep")
ppc_dens_overlay(
y = stan_dat_hier$complaints,
yrep = y_rep[1:200,]
)